package com.lepu.app.fun.health.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

import com.app.utils.BitmapLoadUtil;
import com.app.utils.LoginUtil;
import com.app.utils.Setting;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.core.ApiClient;
import com.core.lib.core.AsyncRequest;
import com.lepu.app.fun.chat.util.ChatConst;
import com.lepu.app.fun.chat.util.ChatUtil;
import com.lepu.app.fun.health.adapter.DurgDetailAdapter;
import com.lepu.app.fun.health.bean.BeanDurgDetail;
import com.lepu.app.main.bean.BeanFocus;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.FocusTwoView;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;
import com.lepu.pasm.R;

public class DurgDetailActivity extends BaseFragmentActivity implements
		OnTopbarNewLeftLayoutListener, AsyncRequest {
	private static final String REQUEST_DATA = "request_data";
	
	private FocusTwoView mFocusView = null; // 轮播图
	private LinearLayout mFocusLayout = null;
	private ArrayList<BeanFocus> mFocusList;
	
	private TextView mName;
	private TextView mInfo;
	private TextView mPrice;
//	private WebView mWebView;
	
	private ListView mListView;
	private View mHeadView = null;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		mNeedSwipeBack = false;
		super.onCreate(savedInstanceState);
		setContentView(R.layout.health_durg_detail_activity);

		init();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {
		finish(true);
	}
	
	@Override
	public void onTopbarLeftLayoutSelected() {
		finish(true);
	}

	private void showHideNoDataLayout(boolean isShow)
	{
		LinearLayout noDataLayout = (LinearLayout) findViewById(R.id.noDataLayout);
		if(noDataLayout != null)
		{
			if(isShow)
			{
				noDataLayout.setVisibility(View.VISIBLE);
				noDataLayout.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						
					}
				});
			}else{
				noDataLayout.setVisibility(View.GONE);
			}
		}
	}
	
	/**
	 * 初始化方法
	 */
	@SuppressLint({ "InlinedApi", "NewApi", "SetJavaScriptEnabled", "JavascriptInterface", "InflateParams" })
	private void init() {
		// topbar
		CustomTopBarNew topbar = (CustomTopBarNew) findViewById(R.id.topbar);
		topbar.setTopbarTitle("商品详情");
		topbar.setonTopbarNewLeftLayoutListener(this);
		
		
		String durgId = "";
		Bundle bundle = getIntent().getExtras();
		if(bundle != null && bundle.size() > 0){
			durgId = (String) bundle.getSerializable("durgId");
		}
		
		
		mHeadView = LayoutInflater.from(DurgDetailActivity.this).inflate(R.layout.durg_detail_head_view, null);
		
		mFocusLayout = (LinearLayout) mHeadView.findViewById(R.id.focus_layout);
		mName = (TextView) mHeadView.findViewById(R.id.name);
		mInfo = (TextView) mHeadView.findViewById(R.id.info);
		mPrice = (TextView) mHeadView.findViewById(R.id.price);
		
//		mWebView = (WebView) findViewById(R.id.webView);
//		
//		mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
//		mWebView.getSettings().setUseWideViewPort(true);
//		mWebView.getSettings().setLoadWithOverviewMode(true);
		
//		mWebView.setVerticalScrollBarEnabled(false);
//		mWebView.setVerticalScrollbarOverlay(false);
//		mWebView.setHorizontalScrollBarEnabled(false);
//		mWebView.setHorizontalScrollbarOverlay(false);
		
		
//		ImageView imageView = new ImageView(DurgDetailActivity.this);
//		imageView.setBackgroundResource(R.drawable.app_icon);
//		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
//		mWebView.addView(imageView, 0, params);
		
		mFocusList = new ArrayList<>();
		
		mListView = (ListView) findViewById(R.id.dataListView);
		
		TextView call = (TextView) findViewById(R.id.call);
		call.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// 聊天界面
				if(!LoginUtil.jumpLogin(DurgDetailActivity.this))
				{
					ChatUtil.jumpChat(DurgDetailActivity.this, null, ChatConst.TYPE_ONLINE_CHAT_ONLINE);
				}
			}
		});
		
		request(durgId);
		showProgressDialog();
	}
	
	
	/**
	 * 请求消息
	 */
	private void request(String id) {
		String url = Setting.getHealthDurgDetailUrl();

		Map<String, Object> map = new HashMap<String, Object>();
		JSONObject params = new JSONObject();
		try {
			params.put("DeviceID", Setting.DEVICEID);
			params.put("ApplicationID", Setting.APPLICATIONID);
		    params.put("UserID", LoginUtil.getUserId());
			params.put("LoginToken", LoginUtil.getLoginToken());
			
			params.put("MedicineID", id);
		} catch (Exception e) {
			
		}
		map.put("ht", params);
		ApiClient.http_post_main(url, map, null, this, REQUEST_DATA, true);
	}

	@SuppressWarnings("deprecation")
	@Override
	public void RequestComplete(Object arg0, Object arg1) {
		hideProgressDialog();
		showHideNoDataLayout(false);
		
		BeanDurgDetail beanDurg = BeanDurgDetail.parseJsonData((String) arg1);
		if(!TextUtils.isEmpty(beanDurg.ProductName)){
			mName.setText(beanDurg.GenericName+"("+beanDurg.ProductName+")");
		}else{
			mName.setText(beanDurg.GenericName);
		}
		
		mInfo.setText(beanDurg.Indication);
		mPrice.setText("￥ " + beanDurg.Price);
		//mWebView.loadData(beanDurg.Introduce,"text/html", "UTF-8");
		
		List<String> imgUrls = beanDurg.ImgUrls;
		
		if(imgUrls == null ){
			
		}else if(imgUrls.size() == 1){
			ImageView imageView = new ImageView(DurgDetailActivity.this);
			imageView.setScaleType(ImageView.ScaleType.FIT_XY);
			
			WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
			int width = wm.getDefaultDisplay().getWidth();
			LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
			params.width = width;
			params.height = width;
			imageView.setLayoutParams(params);
			
			BitmapLoadUtil.displayImage(R.drawable.default_icon_big,imgUrls.get(0),imageView);
			
			mFocusLayout.addView(imageView);
		}else{
			for(int i=0; i<imgUrls.size(); i++){
				BeanFocus focus = new BeanFocus();
				focus.Cover = imgUrls.get(i);
				mFocusList.add(focus);
			}
			
			mFocusView = new FocusTwoView(DurgDetailActivity.this, mFocusList);
		    mFocusLayout.addView(mFocusView);
		}
		
		DurgDetailAdapter adapter = new DurgDetailAdapter(DurgDetailActivity.this, beanDurg.Introduce);
		mListView.addHeaderView(mHeadView);
		mListView.setAdapter(adapter);
	}

	@Override
	public void RequestError(Object arg0, int arg1, String arg2) {
		hideProgressDialog();
		showHideNoDataLayout(true);
	}
	
}
